<?php
namespace field\variable;

use field\variable\BaseField;
use Utils\AjaxMsg;

class DateTimeField extends BaseField {
    public $name;
    public $description;
    public $extra;
    public $timestamp = 0;

    public function __construct($name, $description, $extra){
        $this->name = $name;
        $this->description = $description;
        $this->extra = $extra;
    }

    /**
     * 转换输入数据
     */
    public function parse($data){
        $timestamp = strtotime($data);
        if(!$timestamp){
            $msg = new AjaxMsg();
            $msg->addMsg('error', 'ERR::DATE_FORMAT_INCORRECT', '错误', '日期时间格式错误');
            return $msg;
        } else {
            $this->timestamp = $timestamp;
            return $timestamp;
        }
    }

    /**
     * 转为string输出
     */
    public function toString(){
        return date('Y年m月d日 H:i:s', $this->timestamp);
    }

    /**
     * 转为数据库格式
     */
    public function toDatabase(){
        return date('Y-m-d H:i:s', $this->timestamp);
    }

    /**
     * 获取数据库的数据类型
     */
    public function getDatabaseType(){
        return 'timestamp';
    }

    public static function getBaseName(){
        return '日期和时间';
    }
}